package marcos.com.ahorcado;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class AhorcadoActivity extends Activity {

	ImageView img;
	EditText intro;
	TextView pal;
	Button btn, salir;

	String[] palabras = { "toro", "avestruz", "caballo", "palindromo" };
	String palabraSeleccionada;
	String palabraIncognito;

	int fallo = 0;
	char letra;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		img = (ImageView) findViewById(R.id.ImgFoto);
		img.setImageResource(R.drawable.fallo0);

		intro = (EditText) findViewById(R.id.Texto);

		pal = (TextView) findViewById(R.id.textView);
		pal.setText(setLetras(cogerPalabra()));

		salir = (Button) findViewById(R.id.salir);
		salir.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();

			}
		});

		btn = (Button) findViewById(R.id.button);
		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				if (!intro.getText().toString().trim().equals("")) {

					comparaLetra(intro.getText().charAt(0));

				}
			}
		});

	}

	public String cogerPalabra() {
		int num = (int) (Math.random() * (palabras.length));
		String palabra = palabras[num];
		palabraSeleccionada = palabra;
		return palabra;

	}

	public String setLetras(String palabra) {
		String inicial = "";
		for (int i = 0; i < palabra.length(); i++) {
			inicial += "_ ";
		}
		palabraIncognito = inicial;
		return palabraIncognito;
	}

	public void comparaLetra(char letra) {
		boolean hayLetra = false;
		for (int i = 0; i < palabraSeleccionada.length(); i++) {

			if (letra == palabraSeleccionada.charAt(i)) {
				hayLetra = true;
				char[] palabraMostrar = pal.getText().toString().toCharArray();
				palabraMostrar[i * 2] = palabraSeleccionada.charAt(i);
				palabraIncognito = String.valueOf(palabraMostrar);
				// palabraSeleccionada = String.valueOf(palabraMostrar);
				pal.setText(palabraIncognito);
				intro.setText("");

				if (pal.getText().toString().replace(" ", "")
						.equals(palabraSeleccionada)) {
					img.setImageResource(R.drawable.win);
				}
			}
			intro.setText("");

		}
		if (!hayLetra) {
			dibujarFallo();
		}
	}

	public void dibujarFallo() {
		fallo++;
		switch (fallo) {
		case 1:
			img.setImageResource(R.drawable.fallo1);
			break;
		case 2:
			img.setImageResource(R.drawable.fallo2);
			break;
		case 3:
			img.setImageResource(R.drawable.fallo3);
			break;
		case 4:
			img.setImageResource(R.drawable.fallo4);
			break;
		case 5:
			img.setImageResource(R.drawable.fallo5);
			break;
		case 6:
			img.setImageResource(R.drawable.fallo6);
			double currenttime = System.currentTimeMillis() + 10000;
			boolean isTime = true;
			while (isTime) {
				if (System.currentTimeMillis() > currenttime) {
					isTime = !isTime;
				}
				img.setImageResource(R.drawable.lose);

				break;
			}
		}

	}
}